package zhtt;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import zhtt.demo01.BlogMapper;

import java.io.IOException;
import java.io.InputStream;

/**
 * Created by mengxiaxia on 2017/4/22.
 */
public class SessionFactoryUtil_POOLED {

        private static final String MYBATIS_CONFIG_PATH = "Configuration.xml";

        private static InputStream is = null;

        private static SqlSessionFactory sqlSessionFactory = null;

        static {
            try {
                is = Resources.getResourceAsStream(MYBATIS_CONFIG_PATH);
                sqlSessionFactory = new SqlSessionFactoryBuilder().build(is, "zhtt");
                sqlSessionFactory.getConfiguration().addMapper(BlogMapper.class);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        /**
         */
        private SessionFactoryUtil_POOLED(){};

        /**
         * @return SqlSessionFactory
         */
        public static SqlSessionFactory getSqlSessionFactoryIntance(){

            return sqlSessionFactory;

        }

        /**
         * @return SqlSession
         */
        public static SqlSession getSqlSessionIntance() {
            return sqlSessionFactory.openSession();
        }

}
